package book.items.item30;

import java.util.HashMap;
import java.util.Map;

public enum Operation {

	PLUS("+") {
		@Override
		double apply(double x, double y) {
			return x + y;
		}
	}, 
	MINUS("-") {
		@Override
		double apply(double x, double y) {
			return x - y;
		}
	}, TIMES("*") {
		@Override
		double apply(double x, double y) {
			return x * y;
		}
	}, DEVIDE("/") {
		@Override
		double apply(double x, double y) {
			return x / y;
		}
	};
	
	private final String symbol;
	
	Operation(String symbol) {
		this.symbol = symbol;
	}
	
	abstract double apply(double x, double uy);

	@Override
	public String toString() {
		return symbol;
	}
	private static final Map<String, Operation> stringToEnum = new HashMap<String, Operation>();
	
	static {
		for(Operation o : values()) {
			stringToEnum.put(o.toString(), o);
		}
	}
	
	public static Operation fromString(String symbol) {
		return stringToEnum.get(symbol);
	}
}
